package com.weichen.studentgameexchange.comment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.weichen.studentgameexchange.comment.dao.CommentReportMapper;
import com.weichen.studentgameexchange.comment.entity.CommentReport;
import com.weichen.studentgameexchange.comment.entity.vo.AddCommentReportVo;
import com.weichen.studentgameexchange.comment.entity.vo.ReportVo;
import com.weichen.studentgameexchange.comment.entity.vo.UserReportVo;
import com.weichen.studentgameexchange.comment.service.ICommentReportService;
import com.weichen.studentgameexchange.common.common.Result;
import com.weichen.studentgameexchange.common.enums.AuditStatus;
import com.weichen.studentgameexchange.common.enums.ReportType;
import com.weichen.studentgameexchange.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 惟尘
 * @since 2025-02-05
 */
@Service
public class CommentReportServiceImpl extends ServiceImpl<CommentReportMapper, CommentReport> implements ICommentReportService {

    @Autowired
    CommentReportMapper commentReportMapper;


    /**
     * 分页查询
     *
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<CommentReport> adminPage(String keyword, Integer page, Integer size) {
        Page<CommentReport> commentReportPage = new Page<>(page, size);
        LambdaQueryWrapper<CommentReport> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLambdaQueryWrapper.orderByDesc(CommentReport::getReportId);
        if (!StrUtil.isBlankIfStr(keyword)) {
            try {
                List<Integer> types = ReportType.stringToValue(keyword);
                List<Integer> status = AuditStatus.stringToValue(keyword);
                if (types.size() > 0)
                    types.forEach(value -> {
                        commentLambdaQueryWrapper.or().eq(CommentReport::getReportType, value);
                    });
                else if (status.size() > 0)
                    status.forEach(value -> {
                        commentLambdaQueryWrapper
                                .or()
                                .eq(CommentReport::getAuditStatus, value)
                                .or()
                                .eq(CommentReport::getAuditResult, value);
                    });
            } catch (IllegalArgumentException e) {
                throw new ServiceException("搜索失败，请输入正确的keyword!");
            }
        }
        return commentReportMapper.selectPage(commentReportPage, commentLambdaQueryWrapper);
    }

    /**
     * 查询单个举报信息
     *
     * @param reportId
     * @return
     */
    @Override
    public ReportVo adminSelectOne(Long reportId) {
        CommentReport commentReport = Db.lambdaQuery(CommentReport.class)
                .eq(CommentReport::getReportId, reportId)
                .one();
        if (commentReport == null)
            throw new ServiceException("举报信息不存在！");
        ReportVo reportVo = BeanUtil.copyProperties(commentReport, ReportVo.class);
        return reportVo;
    }

    /**
     * 修改举报状态
     *
     * @param reportVo
     */
    @Override
    public void adminUpadte(ReportVo reportVo) {
        CommentReport commentReport = Db.lambdaQuery(CommentReport.class)
                .eq(CommentReport::getReportId, reportVo.getReportId())
                .one();
        if (commentReport == null)
            throw new ServiceException("举报信息不存在！");
        commentReport.setAuditStatus(reportVo.getAuditStatus());
        commentReport.setAuditResult(reportVo.getAuditResult());
        commentReportMapper.insertOrUpdate(commentReport);
    }

    @Override
    public Result userAdd(AddCommentReportVo addCommentReportVo) {
        CommentReport commentReport = BeanUtil.copyProperties(addCommentReportVo, CommentReport.class);
        CommentReport one = Db.lambdaQuery(CommentReport.class)
                .eq(CommentReport::getCommentId, commentReport.getCommentId())
                .eq(CommentReport::getUserId, commentReport.getUserId())
                .one();
        if (one != null)
            return Result.error("用户已举报，请等待处理");
        commentReportMapper.insert(commentReport);
        return Result.success();
    }

    @Override
    public Result userSelect(Long userId) {
        List<CommentReport> reports = Db.lambdaQuery(CommentReport.class)
                .eq(CommentReport::getUserId, userId)
                .list();
        List<UserReportVo> vos = new ArrayList<>();
        reports.forEach(commentReport -> {
            UserReportVo vo = BeanUtil.copyProperties(commentReport, UserReportVo.class);
            vos.add(vo);
        });
        return Result.success(vos);
    }
}
